package hc.uco

class RiscosController extends BaseController {

	def beforeInterceptor = [action:this.&auth ]
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [riscosInstanceList: Riscos.list(params), riscosInstanceTotal: Riscos.count()]
    }

    def create = {
        def riscosInstance = new Riscos()
        riscosInstance.properties = params
        return [riscosInstance: riscosInstance]
    }

    def save = {
        def riscosInstance = new Riscos(params)
        if (riscosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'riscos.label', default: 'Riscos'), riscosInstance.id])}"
            redirect(action: "show", id: riscosInstance.id)
        }
        else {
            render(view: "create", model: [riscosInstance: riscosInstance])
        }
    }

    def show = {
        def riscosInstance = Riscos.get(params.id)
        if (!riscosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'riscos.label', default: 'Riscos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [riscosInstance: riscosInstance]
        }
    }

    def edit = {
        def riscosInstance = Riscos.get(params.id)
        if (!riscosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'riscos.label', default: 'Riscos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [riscosInstance: riscosInstance]
        }
    }

    def update = {
        def riscosInstance = Riscos.get(params.id)
        if (riscosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (riscosInstance.version > version) {
                    riscosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'riscos.label', default: 'Riscos')] as Object[], "Another user has updated this Riscos while you were editing")
                    //render(view: "edit", model: [riscosInstance: riscosInstance])
                    //return
                }
            }
            riscosInstance.properties = params
			riscosInstance.updatedBy = session.usuario;
            if (!riscosInstance.hasErrors() && riscosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'riscos.label', default: 'Riscos'), riscosInstance.id])}"
                //redirect(action: "show", id: riscosInstance.id)
            }
            //else {
                //render(view: "edit", model: [riscosInstance: riscosInstance])
            //}
        }
        else {
				System.out.println("insert")
				params['id'] = null
				riscosInstance = new Riscos(params)
				riscosInstance.createdBy = session.usuario;
				riscosInstance.updatedBy = riscosInstance.createdBy;
				if (riscosInstance.save(flush: true)) {
					flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'riscos.label', default: 'Riscos'), riscosInstance.id])}"
				} else
					System.out.println("failed")
        }
		chain(action: "manage", id: params.paciente.id, model: [ riscosInstance: riscosInstance ])
    }

    def delete = {
        def riscosInstance = Riscos.get(params.id)
        if (riscosInstance) {
            try {
                riscosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'riscos.label', default: 'Riscos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'riscos.label', default: 'Riscos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'riscos.label', default: 'Riscos'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def manage = {
		def riscosInstance = new Riscos();
		riscosInstance.properties = params;
		
		if (request.method == 'GET') {
			def paciente = Paciente.get(params.id)
			params['paciente.id'] = params.id
			def ri = Riscos.findByPaciente(paciente);
			if (chainModel) {
				def model = chainModel["riscosInstance"]
				riscosInstance = model
				if (ri) {
					riscosInstance.createdBy = ri.createdBy
					riscosInstance.dateCreated = ri.dateCreated
				}
			} else {
				riscosInstance = ri
			}
			
			if (!riscosInstance) {
				riscosInstance = new Riscos();
			}
			
			['pacienteInstance':paciente, riscosInstance: riscosInstance]
		}
	}
}
